/*************************************************************************  
 *   This file is part of UBBMPSREADER.
 *
 *   UBBMPSREADER is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UBBMPSREADER is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with UBBMPSREADER.  If not, see <http://www.gnu.org/licenses/>. 
 *************************************************************************/

package cs.ubbcluj.ro.mps.states;

import java.io.IOException;
import java.io.PushbackInputStream;

import cs.ubbcluj.ro.mps.data.interfaces.Mps;
import cs.ubbcluj.ro.mps.reader.MpsInputStream;
import cs.ubbcluj.ro.mps.states.interfaces.State;
import cs.ubbcluj.ro.mps.states.utils.CharConstants;

/**
 * Represents the RANGES section of the MPS file
 * 
 * @author avadas
 * @version 1.0
 */
public strictfp class RangesState extends AbstractState {

    /** True if we parsed the whole section */
    private boolean endOfSection;

    /** True if has bounds sections */
    private boolean hasBounds = false;

    /** The bounds state */
    private final State boundsState;

    public RangesState(final State nextState, final State boundsState,
	    final State errorState) {
	super(nextState, errorState);
	this.boundsState = boundsState;
    }

    @Override
    public State nextState(final MpsInputStream input, final Mps mps)
	    throws IOException {

	if (consumeFirstFourColumns(input)) {
	    return this.errorState.nextState(input, mps);
	}

	while (!this.endOfSection) {
	    final StringBuffer nameBuffer = new StringBuffer();
	    try {

		if (readVariable(input, mps, nameBuffer)) {
		    return this.errorState.nextState(input, null);
		}
	    } catch (final NumberFormatException e) {
		return this.errorState.nextState(input, null);
	    } catch (final Exception e) {
		return this.errorState.nextState(input, null);
	    }

	    final int in = input.read();
	    if (in == 32) {
		consumeSpaces(input);

		try {
		    readVariable(input, mps, nameBuffer);
		} catch (final NumberFormatException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		} catch (final Exception e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}
		if (input.read() == CharConstants.CR) {
		    input.read();
		}
	    }

	    if (in == CharConstants.CR) {
		input.read();
	    }

	    if (consumeFirstFourColumns(input)) {
		return this.errorState.nextState(input, mps);
	    }
	}
	if (this.hasBounds) {
	    return this.boundsState;
	}
	return this.nextState;
    }

    private boolean readVariable(final MpsInputStream input, final Mps mps,
	    final StringBuffer nameBuffer) throws NumberFormatException,
	    Exception {

	final StringBuffer constrain = new StringBuffer();
	if (nameBuffer.toString().equals("")) {
	    consumeData(input, nameBuffer);

	    consumeSpaces(input);
	}

	consumeData(input, constrain);

	consumeSpaces(input);

	final StringBuffer valueNameBuffer = new StringBuffer();
	consumeData(input, valueNameBuffer);

	try {
	    mps.addConstrainToRanges(constrain.toString(), nameBuffer
		    .toString(), Float.parseFloat(valueNameBuffer.toString()));
	} catch (final NumberFormatException ex) {
	    this.errorState.nextState(input, mps);
	}
	return false;
    }

    /**
     * 
     * @param input
     *            the inputstream
     * @return true if was a parseError
     * @throws IOException
     *             see {@link IOException}
     */
    private boolean consumeFirstFourColumns(final PushbackInputStream input)
	    throws IOException {
	int in = input.read();
	int i = 1;
	while (i < 4) {
	    if (in != 32) {
		if (((char) in == 'E') || ((char) in == 'B')) {
		    this.endOfSection = true;
		    input.unread(in);
		    if (in == 'B') {
			this.hasBounds = true;
		    }
		    return false;

		} else {
		    return true;
		}
	    }
	    i++;
	    in = input.read();
	}
	return false;
    }
}
